package edu.ues21.tcs.ecommerce2011.mvc.controller;

import edu.ues21.tcs.ecommerce2011.mvc.model.Articulo;
import edu.ues21.tcs.ecommerce2011.mvc.model.Marca;
import edu.ues21.tcs.ecommerce2011.mvc.service.CatalogoService;

import java.io.IOException;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @authors Javier Ricardo  Juan Martin
 * @modified 27/10 fmantaras => Quite marcas y cree el controller ListarMarcas
 */
public class ListarArticulosController extends HttpServlet {

    private static final long serialVersionUID = -8654616632048890661L;
    private ApplicationContext ctx;
    private CatalogoService catalogo;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Integer marcaId = null;
        String buscar = null;
        if (request.getParameter("marcaId") != null && !request.getParameter("marcaId").equals("") && !request.getParameter("marcaId").equals("0")) {
            marcaId = new Integer(request.getParameter("marcaId").trim());
        } else if (request.getParameter("buscar") != null && !request.getParameter("buscar").equals("")) {
            buscar = request.getParameter("buscar").trim();
        }

        request.setAttribute("marcaRet", ((marcaId!=null)?marcaId:new Integer(0)));
        request.setAttribute("buscarRet", ((buscar!=null)?buscar:""));

        ctx = new ClassPathXmlApplicationContext("BeanLocations.xml");
        catalogo = (CatalogoService) ctx.getBean("catalogoService");

        List<Articulo> resultado = null;
        List<Marca> marcas = null;

        try {
            if(buscar!=null){
                resultado = catalogo.buscar(buscar);
            }
            else{
                resultado = catalogo.getArticulos(marcaId);
            }
            if(resultado!=null){
                request.setAttribute("resultadoArticulos", resultado);
            }
            

            marcas = catalogo.getMarcas();
            request.setAttribute("marcas", marcas);

            request.getRequestDispatcher("view/ListarArticulosView.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("errorMsg", "Se ha producido un error ejecutando el servicio. Por favor intente más tarde");
            request.getRequestDispatcher("view/error/ErrorMsg.jsp").forward(request, response);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}
